package cn.cmcc.online.smsapi.ui.util;

import android.content.Context;
import android.content.res.Resources;
import android.util.TypedValue;

/**
 * Created by wangyuliang on 2015/10/30.
 */
public class DensityUtil {

    public static float getScreenDensity(Context context) {
        return context.getResources().getDisplayMetrics().density;
    }

    public static int dp2px(Context context, int dp) {
        return (int) (dp * getScreenDensity(context) + 0.5);
    }

    public static int px2dp(Context context, int px) {
        return (int) (px / getScreenDensity(context) + 0.5);
    }

    public static float sp2px(Context c, int sp) {
        Resources r;

        if (c == null) {
            r = Resources.getSystem();
        } else {
            r = c.getResources();
        }
        float textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, r.getDisplayMetrics());
        return textSize;
    }
}
